<?php
	/**
	This file is part of PEL.

	PEL is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
    
	PEL is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
    
	You should have received a copy of the GNU General Public License
	along with PEL.  If not, see <http://www.gnu.org/licenses/>.
	 */
	/**
	 * Elgg pel: add pel action
	 */

	// Make sure we're logged in (send us to the front page if not)
	gatekeeper();

  // Make sure action is secure
  action_gatekeeper();

	// Get input data
  $native = get_input('pelnative');
  $access = get_input('access_id');

	// Cache to the session
	$_SESSION['pelnative'] = $native;

	// Make sure the title / description aren't blank
	if (empty($native)){
		register_error(elgg_echo("pel:blank"));
		forward("mod/pel/add.php");
	// Otherwise, save the pel 
	}
  else {
  	// Initialise a new ElggObject
		$pel = new ElggObject();
    // Tell the system it's a pel
		$pel->subtype = "pel";
    // Set its owner to the current user
		$pel->owner_guid = $_SESSION['user']->getGUID();
    // For now, set its access to public (we'll add an access dropdown shortly)
		$pel->access_id = $access;
    // Set its description appropriately
		$pel->description = $native;
    // Before we can set metadata, we need to save the pel
		if (!$pel->save()){
			register_error(elgg_echo("pel:error"));
			forward("mod/pel/add.php");
		}
    // Success message
		system_message(elgg_echo("pel:saved"));
    // Remove the pel cache
		unset($_SESSION['pelnative']);
    // Forward to the main pel page
		forward("mod/pel/?username=" . $_SESSION['user']->username);
	}
?>
